package com.example.repoprovider.dubbo;
import com.example.repoprovider.service.IRepoService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.RepoApi;
import org.example.dto.RepoDto;
import org.example.exception.BizException;
import org.example.response.R;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Kong
 * @date 2021-8-19 17:09
 */
@Slf4j
@DubboService
public class RepoDubboService implements RepoApi {
    @Autowired
    private IRepoService repoService;
    @Override
    public R decreaseRepo(RepoDto dto) {
        log.info("[AT模式]全局事务XID:{}", RootContext.getXID());
        int rs = repoService.decreaseRepo(dto.getProductCode(),dto.getAccount());
        if(rs >0) {
            return R.Builder.getInstance().buildOk().build();
        }
        throw new BizException("库存不足");
    }
}
